package kuma.ListBtnClick;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class BookAdapter extends ArrayAdapter<Book> {
  private LayoutInflater inflater;
  private int resId;

  public BookAdapter(Context ctx , int resId , List<Book> items){
    super(ctx, resId , items);
    this.resId = resId;
    this.inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    MultiButtonListView list = null;
    try{
      list = (MultiButtonListView)parent;
    }catch(Exception e){
      e.printStackTrace();
    }
    if(convertView == null){
      convertView = inflater.inflate(resId, null);
      holder = new ViewHolder();
      holder.titleView = (TextView)convertView.findViewById(R.id.titleText);
      holder.authorView = (TextView)convertView.findViewById(R.id.authorText);
      holder.button1 = (Button)convertView.findViewById(R.id.titleBtn);
      holder.button2 = (Button)convertView.findViewById(R.id.authorBtn);
      holder.button1.setOnClickListener(list);
      holder.button2.setOnClickListener(list);

      convertView.setTag(holder);
    }
    else{
      holder = (ViewHolder)convertView.getTag();
    }
    Book book = this.getItem(position);
    holder.titleView.setText(book.getTitle());
    holder.authorView.setText(book.getAuthor());
    holder.button1.setTag(position);
    holder.button2.setTag(position);

    return convertView;
  }

  private static class ViewHolder{
    public TextView titleView;
    public TextView authorView;
    public Button button1;
    public Button button2;
  }
}